//
//  DadaAnimationVC.m
//  qqqqqq
//
//  Created by iOSer on 2016/12/21.
//  Copyright © 2016年 iOSer. All rights reserved.
//

#import "DadaAnimationVC.h"
#import "dadaTouchView.h"
@interface DadaAnimationVC()<dadaTouchViewDelegate>
@property(nonatomic,strong) UIImageView *bgView;
@property(nonatomic,strong) UILabel *timeLabel;
@property(nonatomic,strong) UILabel *chePaiLab;
@property (strong,nonatomic) UIView *centerView;
@property(nonatomic,strong) UILabel *hintLab;
@property(nonatomic,strong) UILabel *yanPiaoTextLab;
@property(nonatomic,strong) UIImageView *yanPiaoImg;
@property(nonatomic,strong) UILabel *yanPiaoNumTextLab;
@property(nonatomic,strong) UILabel *yanPiaoNumLab;
@property(nonatomic,strong) UILabel *phoneText;
@property(nonatomic,strong) UIImageView *bgBannerImg;
@property (strong,nonatomic) UIView *overBgView;
@property(nonatomic,strong) UIView *colorBgView;
@property(nonatomic,strong) UILabel *dayPassLabel;
@property(nonatomic,strong) UIButton *evaluateBtn;
@property(nonatomic,strong) UIImageView *hitImg;
@property(nonatomic,strong) UIImageView *carImg;
@property(nonatomic,strong) dadaTouchView *touchView;

@end
@implementation DadaAnimationVC

-(void)viewDidLoad{
    [self.view setBackgroundColor:HEXColor(0xeeeeee, 1)];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = @"嗒嗒电子票";
//    self.navigationController.navigationBar.topItem.title = @"嗒嗒电子票";
    UIButton *backButton = [[UIButton alloc] init];
    
    //    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"icBigbackNor"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"icBigbackNor"] forState:UIControlStateHighlighted];
    [backButton setTitleColor:HEXColor(0xffffff, 1) forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [backButton setTitle:@" 返回" forState:UIControlStateNormal];
    // 自适应尺寸
    [backButton sizeToFit];
    
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
   
    //设置导航条上的item
    self.navigationItem.leftBarButtonItems = @[leftBarButtonItem];
    
}


-(void)viewWillAppear:(BOOL)animated{
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self setupUI];
    [self InfoMake];
    NSString *strRandom = @"";
    
    for(int i=0; i<4; i++)
    {
        strRandom = [ strRandom stringByAppendingFormat:@"%i",(arc4random() % 9)];
    }
    self.yanPiaoNumLab.text = strRandom;
}

-(void)InfoMake{
    NSDictionary *dic = [[GGDataBase shareDataBase] getCacheForKey:DADA_INFO_CACHE_KEY];
    NSString *phone = dic[@"phone"];
    NSString *chepaihao = dic[@"chepaihao"];
    NSString *kouling = dic[@"kouling"];
    NSString *carImgName = dic[@"carImg"];
    if (phone.length!=0) {
        _phoneText.text = [NSString stringWithFormat:@"手机号码:   %@",phone];
    }
    if (chepaihao.length!=0 && ![chepaihao isEqualToString:@"京"]) {
        self.chePaiLab.text = chepaihao;
    }
    if (kouling.length!=0) {
        self.dayPassLabel.text = kouling;
    }
    if (carImgName.length!=0) {
        self.carImg.image = [UIImage imageNamed:carImgName];
    }
    
    NSDate *date12 = [self getCustomDateWithHour:12];
    NSDate *currentDate = [NSDate date];
    
    if ([currentDate compare:date12]==NSOrderedDescending)
    {
        _timeLabel.text =[NSString stringWithFormat:@"%@ 18:50",[[DefaultTools sharedInstance] nowDateWithTime]];
        
    }else{
        _timeLabel.text =[NSString stringWithFormat:@"%@ 07:48",[[DefaultTools sharedInstance] nowDateWithTime]];
    }
}


- (NSDate *)getCustomDateWithHour:(NSInteger)hour
{
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *currentComps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
    
    //设置当天的某个点
    NSDateComponents *resultComps = [[NSDateComponents alloc] init];
    [resultComps setYear:[currentComps year]];
    [resultComps setMonth:[currentComps month]];
    [resultComps setDay:[currentComps day]];
    [resultComps setHour:hour];
    
    NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [resultCalendar dateFromComponents:resultComps];
}

-(void)setupUI{
    [self.view addSubview:self.bgView];
    [self.bgView addSubview:self.timeLabel];
    [self.bgView addSubview:self.chePaiLab];
    [self.bgView addSubview:self.centerView];
    [self.centerView addSubview:self.touchView];
    [self.centerView addSubview:self.yanPiaoTextLab];
    [self.centerView addSubview:self.hintLab];
    [self.centerView addSubview:self.yanPiaoImg];
    [self.bgView addSubview:self.yanPiaoNumTextLab];
    [self.bgView addSubview:self.yanPiaoNumLab];
    [self.bgView addSubview:self.phoneText];
    [self.bgView addSubview:self.bgBannerImg];
    
    [_bgView makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(self.mas_topLayoutGuideBottom).offset(GGRealValue(15));
        make.left.equalTo(self.view).offset(GGRealValue(14));
        make.right.equalTo(self.view).offset(-GGRealValue(14));
        make.bottom.equalTo(self.view).offset(-GGRealValue(140));
    }];
    [_timeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bgView).offset(GGRealValue(34));
        make.left.equalTo(_bgView).offset(GGRealValue(20));
    }];
    [_chePaiLab makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_timeLabel.mas_bottom).offset(GGRealValue(14));
        make.left.equalTo(_bgView).offset(GGRealValue(20));
    }];
    [_centerView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_chePaiLab.mas_bottom).offset(GGRealValue(18));
        make.bottom.equalTo(_bgView).offset(-GGRealValue(61));
        make.left.right.equalTo(_bgView);
    }];
    [_yanPiaoTextLab makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_centerView);
        make.left.equalTo(_centerView).offset(GGRealValue(20));
    }];
    [_yanPiaoImg makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_centerView);
        make.size.equalTo(CGSizeMake(GGRealValue(16), GGRealValue(16)));
        make.right.equalTo(_bgView).offset(-GGRealValue(18));
    }];
    [_hintLab makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_yanPiaoImg.mas_left).offset(-GGRealValue(8));
        make.centerY.equalTo(_yanPiaoImg);
    }];
    [_touchView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_yanPiaoTextLab.mas_bottom).offset(GGRealValue(20));
        make.size.equalTo(CGSizeMake(GGRealValue(175), GGRealValue(175)));
    }];
    [_yanPiaoNumLab makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_bgView).offset(-GGRealValue(18));
        make.bottom.equalTo(_bgView).offset(-GGRealValue(34));
    }];
    [_yanPiaoNumTextLab makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_yanPiaoNumLab);
        make.right.equalTo(_yanPiaoNumLab.mas_left).offset(-GGRealValue(14));
    }];
    [_phoneText makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_yanPiaoNumLab.mas_bottom).offset(GGRealValue(5));
        make.right.equalTo(_bgView).offset(-GGRealValue(18));
    }];
    [_bgBannerImg makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-GGRealValue(28));
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(43));
    }];
    
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)progressAnimationEnd{
    [self.centerView removeFromSuperview];
    [_bgView updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-GGRealValue(90));
    }];
    [_yanPiaoNumLab updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_bgView).offset(-GGRealValue(40));
    }];
    UIView *lineView = [UIView new];
    [lineView setBackgroundColor:HEXColor(0xffffff, 1)];
    [self.bgView addSubview:self.overBgView];
    [self.overBgView addSubview:self.colorBgView];
    [self.colorBgView addSubview:self.dayPassLabel];
    [self.overBgView addSubview:lineView];
    [self.overBgView addSubview:self.evaluateBtn];
    [self.overBgView addSubview:self.hitImg];
    [self.view addSubview:self.carImg];
    
    [_overBgView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_chePaiLab.mas_bottom).offset(GGRealValue(18));
        make.bottom.equalTo(_bgView).offset(-GGRealValue(70));
        make.left.equalTo(_bgView).offset(GGRealValue(1));
        make.right.equalTo(_bgView).offset(GGRealValue(-1));
    }];
    [_colorBgView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_overBgView);
        make.bottom.equalTo(_overBgView).offset(-GGRealValue(84));
        make.left.right.equalTo(_overBgView);
    }];
    [_dayPassLabel makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_colorBgView);
    }];
    [_evaluateBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_colorBgView.mas_bottom).offset(GGRealValue(18));
        make.left.equalTo(_overBgView).offset(GGRealValue(18));
        make.right.bottom.equalTo(_overBgView).offset(-GGRealValue(18));
    }];
    [_hitImg makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(_evaluateBtn);
        make.bottom.equalTo(_evaluateBtn).offset(-GGRealValue(10));
        make.width.equalTo(GGRealValue(70));
    }];
    [lineView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_colorBgView.mas_bottom);
        make.left.right.equalTo(_colorBgView);
        make.bottom.equalTo(_evaluateBtn);
    }];
    [_carImg makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-GGRealValue(20));
        make.left.equalTo(self.view).offset(-GGRealValue(67));
        make.size.equalTo(CGSizeMake(GGRealValue(67), GGRealValue(38)));
    }];
    [self colorBgViewChange];
    [self fruitUIMake];
    [self carImgAnimagiton];
    
}
-(void)colorBgViewChange{
    UIColor *oneColor = HEXColor(0xc2e0ff, 1);
    UIColor *twoColor = HEXColor(0xc6ffe1, 1);
    UIColor *threeColor = HEXColor(0xfff8c6, 1);
    UIColor *fourColor = HEXColor(0xffcbf0, 1);
    
   
    [UIView animateWithDuration:0.75 animations:^{
        self.colorBgView.backgroundColor = oneColor;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.75 animations:^{
            self.colorBgView.backgroundColor =twoColor;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.75 animations:^{
                self.colorBgView.backgroundColor = threeColor;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.75 animations:^{
                    self.colorBgView.backgroundColor =fourColor;
                } completion:^(BOOL finished) {
                    [self colorBgViewChange];
                }];
            }];
        }];
    }];
 
}
-(void)fruitUIMake{
    NSArray *imgNameArray= [self randomArray];
    [self.colorBgView layoutIfNeeded];
    NSArray *widthArray = [[NSArray alloc] initWithObjects:
                           @(GGRealValue(34)),@(GGRealValue(36)),@(GGRealValue(52)),@(GGRealValue(33)),@(GGRealValue(43)),@(GGRealValue(45)), nil];
    float allWith = 0;
    for (int i = 0; i<widthArray.count; ++i) {
        allWith = allWith + [widthArray[i] floatValue];
    }
    float margin = (self.colorBgView.frame.size.width - allWith)/7;
    
    
    for (int i = 0; i<imgNameArray.count; ++i) {
        NSDictionary *dic = imgNameArray[i];
        UIImageView *fruitImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:dic[@"name"]]];
        float marginWith = 0;
        for (int a = 0; a<i; ++a) {
            marginWith = marginWith + [widthArray[a] floatValue];
        }
        float withNum = [widthArray[i] floatValue];
        float scaleNum = [dic[@"scale"] floatValue];
        float heighNum = withNum*scaleNum;
        [self.colorBgView addSubview:fruitImg];
        
        [fruitImg makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.colorBgView);
            make.left.equalTo(self.colorBgView).offset(margin+marginWith+(i*margin));
            make.width.equalTo(withNum);
            make.height.equalTo(heighNum);
        }];
        [fruitImg layoutIfNeeded];
        float animTime = [dic[@"animTime"] floatValue];
        [self fruitAnimation:fruitImg time:animTime];
    }
    
}
-(void)carImgAnimagiton{
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGAffineTransform transformTo = CGAffineTransformMakeTranslation(SCREEN_WIDTH+GGRealValue(150), 0);
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView animateWithDuration:7 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        _carImg.transform = transformTo;
    } completion:^(BOOL finished) {
        if (finished) {
            
            _carImg.transform = transform;
            [self carImgAnimagiton];
        }
    }];
}
- (void)fruitAnimation:(UIImageView *)imageView time:(float)time
{

    CGAffineTransform transform = CGAffineTransformIdentity;
    CGAffineTransform transformTo = CGAffineTransformMakeTranslation(0, self.colorBgView.frame.size.height );
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView animateWithDuration:time delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        imageView.transform = transformTo;
    } completion:^(BOOL finished) {
        if (finished) {
            
            imageView.transform = CGAffineTransformMakeTranslation(transform.tx, transform.ty-10);
            [self fruitAnimation:imageView time:time];
        }
    }];
}

//0.85 0.68 0.6 0.45 0.66 0.78 //7
-(NSArray *)randomArray
{
    //随机数从这里边产生
    NSMutableArray *startArray=[[NSMutableArray alloc] initWithObjects:
                                @{@"name":@"dada_apple",@"scale":[NSNumber numberWithFloat:0.758],@"animTime":[NSNumber numberWithFloat:0.58]},
                                @{@"name":@"dada_banana",@"scale":[NSNumber numberWithFloat:0.698],@"animTime":[NSNumber numberWithFloat:0.68]},
                                @{@"name":@"dada_grape",@"scale":[NSNumber numberWithFloat:1.121],@"animTime":[NSNumber numberWithFloat:0.6]},
                                @{@"name":@"dada_orange",@"scale":[NSNumber numberWithFloat:0.904],@"animTime":[NSNumber numberWithFloat:0.45]},
                                @{@"name":@"dada_watermelon",@"scale":[NSNumber numberWithFloat:0.694],@"animTime":[NSNumber numberWithFloat:0.66]},
                                @{@"name":@"dadas_mangosteen",@"scale":[NSNumber numberWithFloat:0.875],@"animTime":[NSNumber numberWithFloat:0.78]},
                                nil];
    //随机数产生结果
    NSMutableArray *resultArray=[[NSMutableArray alloc] initWithCapacity:0];
    //随机数个数
    NSInteger m=6;
    for (int i=0; i<m; i++) {
        int t=arc4random()%startArray.count;
        resultArray[i]=startArray[t];
        startArray[t]=[startArray lastObject]; //为更好的乱序，故交换下位置
        [startArray removeLastObject];
    }
    return resultArray;
}
-(dadaTouchView *)touchView{
    if (!_touchView) {
        _touchView = [[dadaTouchView alloc] initWithFrame:CGRectMake(0, 0, GGRealValue(175), GGRealValue(175))];
        _touchView.deledate = self;
    }
    return _touchView;
}
-(UIImageView *)bgView{
    if (!_bgView) {
        _bgView = [UIImageView new];
        _bgView.userInteractionEnabled = YES;
        _bgView.image = [UIImage imageNamed:@"dadaBg"];
    }
    return _bgView;
}
-(UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:GGRealValue(16)];
        _timeLabel.textColor = HEXColor(0x666666, 1);
        _timeLabel.text = @"12月22日 18:50";
        [_timeLabel sizeToFit];
    }
    return _timeLabel;
}
-(UILabel *)chePaiLab{
    if (!_chePaiLab) {
        _chePaiLab = [UILabel new];
        _chePaiLab.font = [UIFont systemFontOfSize:GGRealValue(34) weight:UIFontWeightSemibold ];
        _chePaiLab.textColor = HEXColor(0x2b2b2b, 1);
        _chePaiLab.text = @"京AX5183";
        [_chePaiLab sizeToFit];
    }
    return _chePaiLab;
}
-(UILabel *)yanPiaoTextLab{
    if (!_yanPiaoTextLab) {
        _yanPiaoTextLab = [UILabel new];
        _yanPiaoTextLab.textColor = HEXColor(0x2b2b2b, 1);
        _yanPiaoTextLab.text = @"验票后将无法退票";
        _yanPiaoTextLab.font = [UIFont systemFontOfSize:GGRealValue(18) weight:UIFontWeightRegular];
        [_yanPiaoTextLab sizeToFit];
    }
    return _yanPiaoTextLab;
}
-(UILabel *)hintLab{
    if (!_hintLab) {
        _hintLab = [[UILabel alloc] init];
        _hintLab.font = [UIFont fontWithName:@"MicrosoftYaHei" size:GGRealValue(14)];
        _hintLab.textColor = HEXColor(0x999999, 1);
        _hintLab.text = @"验票";
        [_hintLab sizeToFit];
    }
    return _hintLab;
}
-(UIImageView *)yanPiaoImg{
    if (!_yanPiaoImg) {
        _yanPiaoImg = [UIImageView new];
        _yanPiaoImg.image = [UIImage imageNamed:@"dada_help"];
    }
    return _yanPiaoImg;
}

-(UILabel *)yanPiaoNumTextLab{
    if (!_yanPiaoNumTextLab) {
        _yanPiaoNumTextLab = [UILabel new];
        _yanPiaoNumTextLab.text = @"验票码:";
        _yanPiaoNumTextLab.font = [UIFont fontWithName:@"MicrosoftYaHei" size:GGRealValue(14)];
        _yanPiaoNumTextLab.textColor = HEXColor(0x666666, 1);
        [_yanPiaoNumTextLab sizeToFit];
    }
    return _yanPiaoNumTextLab;
}
-(UILabel *)yanPiaoNumLab{
    if (!_yanPiaoNumLab) {
        _yanPiaoNumLab = [UILabel new];
        _yanPiaoNumLab.text = @"6610";
        _yanPiaoNumLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:GGRealValue(16)];
        _yanPiaoNumLab.textColor = HEXColor(0x666666, 1);
        [_yanPiaoNumLab sizeToFit];
    }
    return _yanPiaoNumLab;
}
-(UILabel *)phoneText{
    if (!_phoneText) {
        _phoneText = [UILabel new];
        _phoneText.text = @"手机号码:   18649057065";
        _phoneText.font = [UIFont fontWithName:@"MicrosoftYaHei" size:GGRealValue(14)];
        _phoneText.textColor = HEXColor(0x666666, 1);
        [_phoneText sizeToFit];
    }
    return _phoneText;
}
-(UIImageView *)bgBannerImg{
    if (!_bgBannerImg) {
        _bgBannerImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dada_bgBanner"]];
    }
    return _bgBannerImg;
}
-(UIView *)centerView{
    if (!_centerView) {
        _centerView = [UIView new];
        [_centerView setBackgroundColor:HEXColor(0xffffff, 1)];
    }
    return _centerView;
}
-(UIView *)overBgView{
    if(!_overBgView){
        _overBgView = [UIView new];
        [_overBgView setBackgroundColor:HEXColor(0xffffff, 0)];
        
    }
    return _overBgView;
}
-(UIView *)colorBgView{
    if (!_colorBgView) {
        _colorBgView = [UIView new];
        [_colorBgView setBackgroundColor:HEXColor(0xc2e0ff, 1)];
    }
    return _colorBgView;
}
-(UILabel *)dayPassLabel{
    if (!_dayPassLabel) {
        _dayPassLabel = [[UILabel alloc] init];
        _dayPassLabel.text = @"手气最佳";
        _dayPassLabel.textColor = HEXColor(0x0757ab, 1);
        _dayPassLabel.font = [UIFont systemFontOfSize:GGRealValue(78) weight:UIFontWeightBold];
    }
    return _dayPassLabel;
}
-(UIButton *)evaluateBtn{
    if (!_evaluateBtn) {
        _evaluateBtn = [[UIButton alloc] init];
        [_evaluateBtn setTitle:@"评价一下" forState:UIControlStateNormal];
        [_evaluateBtn setBackgroundColor:HEXColor(0xffffff, 1)];
        _evaluateBtn.titleLabel.font = [UIFont systemFontOfSize:GGRealValue(20)];
        [_evaluateBtn setTitleColor:HEXColor(0xff9c00, 1) forState:UIControlStateNormal];
        _evaluateBtn.layer.cornerRadius = GGRealValue(5);
        _evaluateBtn.layer.borderColor = HEXColor(0xff9c00, 1).CGColor;
        _evaluateBtn.layer.borderWidth = 1;
    }
    return _evaluateBtn;
}
-(UIImageView *)hitImg{
    if (!_hitImg) {
        _hitImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dada_hint"]];
    }
    return _hitImg;
}
-(UIImageView *)carImg{
    if (!_carImg) {
        _carImg = [UIImageView new];
        _carImg.image = [UIImage imageNamed:@"dada_redBag"];
    }
    return _carImg;
}
@end
